package cn.zyq.ykb.service.task.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.task.TaskStepMapper;
import cn.zyq.ykb.beans.task.TaskStep;
import cn.zyq.ykb.service.task.TaskStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 任务-步骤Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-05-22
 */
@Service
public class TaskStepServiceImpl  extends BaseService implements TaskStepService {

    @Autowired
    private TaskStepMapper taskStepMapper;

    /**
     * 查询任务-步骤
     *
     * @param taskStepID 任务-步骤主键
     * @return 任务-步骤
     */
    @Override
    public TaskStep selectTaskStepByTaskStepID(Integer taskStepID) {
        TaskStep taskStep = taskStepMapper.selectByPrimaryKey(taskStepID);
        if (taskStep.getDeleted() == 1) {
            return null;
        }
        return taskStep;
    }

    /**
     * 查询任务-步骤
     *
     * @param serviceID 业务ID
     * @return 任务-步骤
     */
    @Override
    public TaskStep selectTaskStepByServiceID(String serviceID) {
        TaskStep param = new TaskStep();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.taskStepMapper.selectOne(param);
    }

    /**
     * 查询任务-步骤列表
     *
     * @param pageParam
     * @return 任务-步骤
     */
    @Override
    public PageInfo<TaskStep> selectTaskStepList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<TaskStep> list = this.taskStepMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询任务-步骤列表
     *
     * @param param
     * @return 任务-步骤
     */
    @Override
    public List<TaskStep> selectTaskStepList(TaskStep param) {
        param.setDeleted(0);
        return this.taskStepMapper.select(param);
    }
    /**
     * 新增任务-步骤
     *
     * @param param 任务-步骤
     * @return 结果
     */
    @Override
    public int insertTaskStep(TaskStep param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.taskStepMapper.insertSelective(param);
    }

    /**
     * 修改任务-步骤
     *
     * @param param 任务-步骤
     * @return 结果
     */
    @Override
    public int updateTaskStep(TaskStep param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.taskStepMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除任务-步骤
     *
     * @param taskStepIDs 需要删除的任务-步骤主键
     * @return 结果
     */
    @Override
    public int deleteTaskStepByTaskStepIDs(Integer[] taskStepIDs) {
        int i = 0;
        for (Integer taskStepID : taskStepIDs) {
            i += this.deleteTaskStepByTaskStepID(taskStepID);
        }
        return i;
    }

    /**
     * 删除任务-步骤信息
     *
     * @param taskStepID 任务-步骤主键
     * @return 结果
     */
    @Override
    public int deleteTaskStepByTaskStepID(Integer taskStepID) {
        TaskStep taskStep = this.selectTaskStepByTaskStepID(taskStepID);
        if (taskStep == null) {
            return 0;
        } else {
            TaskStep storeInfo = new TaskStep();
            storeInfo.setTaskStepID(taskStepID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.taskStepMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
